package com.itheima.behaviour.service.impl;

import behaviour.dto.FollowBehaviorDto;
import com.itheima.behaviour.mapper.ApBehaviorEntryMapper;
import com.itheima.behaviour.pojo.ApFollowBehavior;
import com.itheima.behaviour.mapper.ApFollowBehaviorMapper;
import com.itheima.behaviour.service.ApFollowBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.constants.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @description <p>APP关注行为 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.behaviour.service.impl
 */
@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {
    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;

    /**
     * 保存用户行为
     *
     * @param dto
     */
    @Override
    public void saveFollowBehavior(FollowBehaviorDto dto) {
        //1.查询行为实体，在这里，我们使用的是用户id，如果用户没登录，是不给关注的。所以我们查询时用的type是用户
        com.itheima.behaviour.pojo.ApBehaviorEntry entry = apBehaviorEntryMapper.findByEntryIdAndType(dto.getUserId(), SystemConstants.TYPE_USER);
        if(null != entry){
            // 如果实体不存在，则不处理
            // 否则构建pojo，并添加到表中
            ApFollowBehavior pojo = new ApFollowBehavior();
            pojo.setArticleId(dto.getArticleId());
            pojo.setEntryId(entry.getId());
            pojo.setFollowId(dto.getFollowId());
            pojo.setCreatedTime(LocalDateTime.now());
            save(pojo);
        }
    }
}
